#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author  : mh

'''
Configuration
'''

import config_default


class Dict(dict):
	'''
	重写属性设置，取值方法
	支持通过属性名访问键值对的值，属性名将会当做键名
	'''

	def __init__(self , names = () , values = () , **kw):
		super(Dict , self).__init__(**kw)
		# zip函数将参数数据分组返回[(arg1[0],arg2[0],arg3[0]...),(arg1[1],arg2[1],arg3[1]...),,,]
		# 以参数中元素数量最少的集合长度为返回列表长度
		for k , v in zip(names , values):
			self[k] = v

	def __getattr__(self , item):
		try:
			return self[item]
		except KeyError:
			raise AttributeError(r"'Dict' object has no attribute '%s'" % item)

	def __setattr__(self , key , value):
		self[key] = value


def merge(defaults , override):
	r = {}
	for k , v in defaults.items():
		if k in override:
			if isinstance(v , dict):
				r[k] = merge(v , override[k])
			else:
				r[k] = override[k]
		else:
			r[k] = v
	return r


def toDict(d):
	D = Dict()
	for k , v in d.items():
		D[k] = toDict(v) if isinstance(v , dict) else v
	return D


configs = config_default.configs

try:
	import config_override

	configs = merge(configs , config_override.configs)
except ImportError:
	pass

configs = toDict(configs)
